import 'package:flutter/cupertino.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class PasswordChangePageViewModel extends BaseViewModel {
  String? getAccount() {
    return getApplication().getLoginAccount();
  }

  // 检查密码是否错误
  Future<bool?> checkPassword({required String password}) async {
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    params['password'] = (await getChannelService()
            .getCommonChannelFunc()
            ?.convertSHA256(password)) ??
        '';
    final result = await getNetworkUtil()
        .postHttp(UrlContract.checkUserPassword, params: params);
    final resp = CommonResponse.fromMap(result);
    return resp.trueOrFalse;
  }

  bool check2Password(String first, String second) {
    return first == second;
  }

  // 更改密码
  Future<void> changePassword({
    required String newPassword,
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    final params = <String, Object>{};
    params['account'] = getAccount() ?? '';
    params['password'] = (await getChannelService()
            .getCommonChannelFunc()
            ?.convertSHA256(newPassword)) ??
        '';

    final result =
        await getNetworkUtil().postHttp(UrlContract.modifyUserPassword, params: params);
    final resp = CommonResponse.fromMap(result);
    if (resp.status == 0) {
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
    // 回馈信息
  }

  Future<void> backToPage(BuildContext context) async {
    getApplication().getRouter()?.pop(context);
  }
}
